<?php
/**
 * Created by PhpStorm.
 * User: HUNG NGUYEN
 * Date: 1/16/14
 * Time: 9:36 AM
 */
require_once('db_config.php');


class BanController {
    public function __construct()
    {

    }
    // input is danh sach cac ban can gop
    public static function gopBan($bans)
    {
        /*logic:
            dau vao la 1 danh sach cac ban can gop.
            trong do co it nhat 1 ban dang co nguoi ngoi.
        */

    }
    // input is danh sach hai ban can doi cho nhau
    public static function doiBan($ban1, $ban2)
    {
        /*logic:
        dau vao la hai ban, trong do co it nhat 1 ban dang co nguoi ngoi.
        hai ban se doi hoa don cho nhau.
        */
        try
        {

            /* Open a connection */
            $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

            $response=array('success'=>1);
            $query_update1="update bkxuatban set soban='11111' where soban='$ban1'";
            $query_update2="update bkxuatban set soban='22222' where soban='$ban2'";
            $query_update3="update bkxuatban set soban='$ban2' where soban='11111'";
            $query_update4="update bkxuatban set soban='$ban1' where soban='22222'";

            $mysqli->query($query_update1);
            $mysqli->query($query_update2);
            $mysqli->query($query_update3);
            $mysqli->query($query_update4);

            $mysqli->close();
        }
        catch(Exception $e)
        {
            $response['success']=0;
            $response['errorcode']=$e->getMessage();
        }
        echo json_encode($response);
    }
    // input is ban can tach
    public static function tachBan($ban)
    {
        /* logic:
            dau vao la 1 ban, can tach ban nay thanh hai de cho hai nguoi ngoi, hoac cho hai hoa don khac nhau.
            ==> mot ban moi se duoc sinh ra voi ma hoa don moi.
         */
        try
        {
            $response['success']=1;
            /* Open a connection */
            $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

            $get_ban_record="select * from dmban where tenban='$ban'";
            $ban_record=$mysqli->query($get_ban_record)->fetch_assoc();
            $tenban=$ban."_%";

            $ban_extend="select count(*) as count from dmban where tenban like '$tenban'";
            $number=$mysqli->query($ban_extend)->fetch_assoc()['count'];

            $number+=1;
            $tenban=$ban."_$number";
            $khuvuc=$ban_record['khuvuc'];

            // insert new record
            $new="insert into dmban (tenban,khuvuc,trangthai) values ('$tenban',$khuvuc,0)";
            $mysqli->query($new);

            $mysqli->close();

            echo json_encode($response);
        }
        catch(Exception $e)
        {
            $response['success']=0;
            $response['errorcode']=$e->getMessage();

            echo json_encode($response);
        }


    }

} 